package omm.domain.generalgraph;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import omm.domain.Graphic;
import omm.domain.Quotation;
import omm.domain.Quotation.State;
import omm.util.Graphics;

import org.joda.time.DateTime;

/**
 * Monetary carga un map con las claves: pendentpercentaje: El porcentaje de
 * pendientes wonpercentaje: El porcentaje de ganadas lostpercentaje: El
 * porcentaje de perdidas
 */

public class MonetaryGraphic implements Graphic {
	private String urlformat = "http://chart.apis.google.com/chart?chs=430x200" + "&amp;chd=t:%d,%d,%d&amp;cht=p"
			+ "&amp;chl=Pendiente(%d%%)|Ganada(%d%%)|Perdida(%d%%)&amp;chco=8b0027&amp;chtt=%s&amp;chts=000000,20"
			+ "&amp;chxs=0,000000,18&amp;chxt=x";

	private List<Quotation> quotations;
	final private DateTime beginning;
	final private DateTime ending;
	private Map<Quotation.State, Double> commisionSumMap;

	public MonetaryGraphic(List<Quotation> quotations, DateTime beginning, DateTime ending) {
		this.quotations = quotations;
		this.beginning = beginning;
		this.ending = ending;
		this.commisionSumMap = calculateCommisionSumMap();
	}
	
	public String getGraphicURL() {
		Map<Quotation.State, Integer> map = calculateGraphicAttributes();

		int quoted = 0;
		int won = 0;
		int lost = 0;
		String title = Graphics.makeTitle("MONETARIO",this.beginning,this.ending);
		if(map != null){
			quoted = map.get(State.QUOTED);
			won = map.get(State.WON);
			lost = map.get(State.LOST);
		}
		
		Object[] params = { quoted, won, lost, quoted, won, lost, title };
		return String.format(urlformat, params);
	}
	
	
	public Map<Quotation.State, Double> getCommisionDataMap(){
		return commisionSumMap;
	}
	
	

	private Map<Quotation.State, Double> calculateCommisionSumMap(){
		Map<Quotation.State, Double> map = initCommisionSumMap();
		Iterator<Quotation> it = quotations.iterator();
		// int i=0;
		while (it.hasNext()) {
			Quotation q = it.next();
			Double commision = q.getAonCommision();
			Double actualValue = map.get(q.getState());
		
			if(commision != 0.0 && commision != null)
				map.put(q.getState(), actualValue + commision);

		}
		
		return map;
	}
	
	
	private Map<Quotation.State, Integer> calculateGraphicAttributes() {

		Double total = commisionSumMap.get(State.QUOTED) + commisionSumMap.get(State.WON)
				+ commisionSumMap.get(State.LOST);
		if (total == 0.0)
			return null;
		
		
		// calculo de los porcentajes
		Integer quotedPercent = (int)(Math.round((commisionSumMap.get(State.QUOTED)/total)*100));
		Integer wonPercent = (int)(Math.round((commisionSumMap.get(State.WON)/total)*100));
		Integer lostPercent = (int)(Math.round((commisionSumMap.get(State.LOST)/total)*100));
				
		//Del perl! Redondeo
		int porc_total_entero = 0;
		porc_total_entero += quotedPercent + wonPercent + lostPercent ;
		
		
		Map<Quotation.State, Integer> percentajesMap = initPercentagesMap();
		percentajesMap.put(State.QUOTED, quotedPercent);
		percentajesMap.put(State.WON,wonPercent);
		percentajesMap.put(State.LOST,lostPercent);
		
		
		int dif = 0;
		State state = null;
		int max_portion_mon = 0;
		if ( porc_total_entero != 100 ){
			for (Quotation.State s : Quotation.State.values()) {
				if((s == State.LOST) || (s == State.WON) || (s == State.QUOTED))
					if (percentajesMap.get(s) >= max_portion_mon){
						max_portion_mon = percentajesMap.get(s);
						state = s; 
				}
			}
			dif = porc_total_entero - 100;
			percentajesMap.put(state, percentajesMap.get(state)-dif);
		}
		
		return percentajesMap;

	}

	private Map<State, Integer> initPercentagesMap() {
		Map<State, Integer> percentajesMap = new HashMap<Quotation.State, Integer>();
		for (Quotation.State s : Quotation.State.values()) {
			percentajesMap.put(s, 0);
		}
		return percentajesMap;
	}

	private Map<Quotation.State, Double> initCommisionSumMap() {
		Map<Quotation.State, Double> commisionSumMap = new HashMap<Quotation.State, Double>();
		for (Quotation.State s : Quotation.State.values()) {
			commisionSumMap.put(s, 0.0);
		}

		return commisionSumMap;
	}
	


}
